package com.jijidom.order.client;

import com.jijidom.order.dto.CarDTO;
import com.jijidom.order.entity.ProductInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * @author JinTao
 * @version 1.0
 * 类说明
 * @Date
 */
@RestController
@Slf4j
public class ClientController {

	@SuppressWarnings("unused")
	@Autowired
    private LoadBalancerClient loadBalancerClient;

	@SuppressWarnings("unused")
	@Autowired
    private RestTemplate Template;

    @Autowired
    private ProductClient productClient;

    @GetMapping("/getProductMsg")
    public String getProductMsg(){
        //第一种方式
        /*RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://localhost:9088/product/msg",String.class);
        log.info("response={}",response);

        //第二种方式(利用LoadBalancerClient获取应用名url,然后再使用RestTemplate)
        ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT");
        serviceInstance.getHost();
        serviceInstance.getPort();
        String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort())+"/product/msg";
        String response2 = restTemplate.getForObject(url,String.class);
        log.info("response={}",response2);

        //第三种方式(将loadBalancerClient注册为服务，自动配置)
        //RestTemplate默认采用轮询的方式请求接口
        String response3 = Template.getForObject("http://PRODUCT/product/msg",String.class);
        log.info("response={}",response3);*/

        String response = productClient.productMsg();
        return response;
    }

    @GetMapping("/ListForOrder")
    public String getProductList(){
        List<ProductInfo> list = productClient.listForOrder(Arrays.asList("157875196366160022","157875227953464068"));
        log.info(list.toString());
        return "ok";
    }

    @GetMapping("/DecreaseStock")
    public String productDecreaseStock(){
        productClient.decreaseStock(Arrays.asList(new CarDTO("157875227953464068",3)));
        return "ok";
    }

}
